export default {
  data() {
    return {
      dataListURL: '',
      // 查询条件
      dataForm: {},
      // 排序，asc 升序/desc 降序
      order: '',
      // 排序，字段
      orderField: '',
      // 当前页码
      page: 1,
      // 每页数
      limit: 10,
      // 总条数
      total: 0,
      // 数据列表
      dataList: [],

      deleteURL: '',
      deleteIsBatchKey: 'id',
      // 数据列表，多选项
      dataListSelections: [],

      // 数据列表，loading状态
      dataListLoading: false,
      // 当有数据的时候是否刷新
      activatedRefresh: true,

      isCanDelete: true,

      sortProperty: ''

      // 转换数据格式,自定义函数
      // transformFunc: null
    }
  },
  created() {
    this.getDataList()
  },
  activated() {
    this.getDataList()
  },
  methods: {
    /**
         * 分页查询
         */
    getDataList() {
      if (this.activatedRefresh || !this.dataList.length) {
        this.dataListLoading = true

        if (this.beforeGetDataList) {
          this.beforeGetDataList()
        }

        // 阻止静态页面访问空链接报错隐患
        if (!this.dataListURL) { return }

        this.$http
          .get(this.dataListURL, {
            params: {
              order: this.order,
              orderField: this.orderField,
              page: this.page,
              limit: this.limit,
              taskId: this.$route.query.taskId,
              ...this.dataForm
            }
          })
          .then((res) => {
            console.log(223, res)
            if (res.code !== 200) { return }
            // 是否分页自行处理数据
            if (res.data.totalCount == null || res.data.totalCount == undefined) {
              return res.data
            } else {
              this.total = res.data.totalCount
              return res.data.list
            }
          })
          .then(list => {
            this.dataList = list
            this.dataListLoading = false
          })
          .catch(e => {
            this.dataListLoading = false
            this.$message.error(e)
          })
      }
    },
    /**
         * 多选
         * @param val
         */
    dataListSelectionChangeHandle(val) {
      this.dataListSelections = val

      if (this.dataListSelections.length > 0) {
        this.isCanDelete = false
      } else {
        this.isCanDelete = true
      }
    },
    /**
         * 排序
         * @param data
         * @return {boolean}
         */
    dataListSortChangeHandle(data) {
      console.log(data)
      if (!data.order || !data.prop) {
        this.order = ''
        this.orderField = ''
        return false
      }
      this.order = data.order.replace(/ending$/, '')
      this.orderField = 'mt.' + data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      // if (data.order === 'desc') {           // 降序
      //     this.dataList = this.dataList.sort(this.myDescSort(data.prop));
      // } else if (data.order === 'asc') {     // 升序
      //     this.dataList = this.dataList.sort(this.myAscSort(data.prop));
      // }
      this.getDataList()
    },
    /**
         * 普通排序（大部分页面都可以用)
         * @param data
         * @return {boolean}
         */
    commonSortChangeHandle(data) {
      console.log(data)
      if (!data.order || !data.prop) {
        this.order = ''
        this.orderField = ''
        return false
      }
      this.order = data.order.replace(/ending$/, '')
      this.orderField = data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      // if (data.order === 'desc') {           // 降序
      //     this.dataList = this.dataList.sort(this.myDescSort(data.prop));
      // } else if (data.order === 'asc') {     // 升序
      //     this.dataList = this.dataList.sort(this.myAscSort(data.prop));
      // }
      this.getDataList()
    },
    /**
         * 特殊排序（人员管理页面)
         * @param data
         * @return {boolean}
         */
    specialSortChangeHandle(data) {
      console.log(data)
      if (!data.order || !data.prop) {
        this.order = ''
        this.orderField = ''
        return false
      }
      this.order = data.order.replace(/ending$/, '')
      let orderFieldName = ''
      switch (data.prop) {
        case 'role.name':
          orderFieldName = 'roleId'
          break
        case 'department.name':
          orderFieldName = 'departmentId'
          break
      }
      this.orderField = orderFieldName !== '' ? orderFieldName.replace(/([A-Z])/g, '_$1').toLowerCase() : data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()
      // if (data.order === 'desc') {           // 降序
      //     this.dataList = this.dataList.sort(this.myDescSort(data.prop));
      // } else if (data.order === 'asc') {     // 升序
      //     this.dataList = this.dataList.sort(this.myAscSort(data.prop));
      // }
      this.getDataList()
    },
    // 升序排列 - add time for 20191024 - jtj
    myAscSort(label) {
      return function(a, b) {
        if (a[label].match(/^[a-zA-Z0-9\u4e00-\u9fa5]+$/g)) { // 判断中文与字母
          return a[label].localeCompare(b[label], 'zh')
        }
        return a[label] - b[label]
      }
    },
    /**
         * 分页, 每页条数
         * @param val
         */
    pageSizeChangeHandle(val) {
      this.page = 1
      this.limit = val
      this.getDataList()
    },
    /**
         * 分页, 当前页
         * @param val
         */
    pageCurrentChangeHandle(val) {
      this.page = val
      this.getDataList()
    },
    /**
         * 跳转
         * @param path
         * @param query
         */
    jumpHandle(path, query) {
      this.$router.push({ path, query })
    },
    /**
         *  删除
         * @param id
         */
    deleteHandle(id) {
      this.$confirm('此操作将删除该组数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).then(() => {
        this.$http
          .delete(this.deleteURL, {
            data: id ? [id] : this.dataListSelections.map(item => item[this.deleteIsBatchKey])
          })
          .then(res => {
            if (res.code == 200) {
              this.$message({
                message: `操作成功！`,
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(res.message)
            }
          })
          .catch(() => {

          })
      })
    }
  }
}
